package com.baosight.sczx.zm.cd.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo; 
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cd.Documents; 
import com.baosight.sczx.common.zm.domain.ZMCDFiles; 

/**
 * <p>Title: ServicetZMCDFiles.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author zhangkun
 * @version 1.0
 * <pre>Histroy:
 *          2010-12-15 下午05:40:29  zhangkun  Create
 *</pre>
 */
public class ServiceZMCD05 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24下午01:04:01 zhangkun
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		// TODO Auto-generated method stub
		return query(inInfo);
	}
	
	/**
	 * 查询文件
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24下午01:03:51 zhangkun
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {  
		String fdForeignId = inInfo.getString("fdForeignId");
		if(fdForeignId==null||fdForeignId.trim().equals("")){
			fdForeignId=inInfo.getCellStr("inqu_status", 0, "fdForeignId");
		}
		if(fdForeignId!=null)
		{
			StaticTools.setQueryParameters(inInfo, "fdForeignId", fdForeignId, true);//添加查询条件
		} 
		EiInfo outInfo=inInfo;
		if(fdForeignId!=null)
			outInfo = super.query(inInfo,"tZMCDFiles.query", new ZMCDFiles());
		else
			outInfo.addBlock("result").setBlockMeta(new ZMCDFiles().eiMetadata); 
		return outInfo;
	}
	
	/**
	 * 删除文件
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24下午01:03:40 zhangkun
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = super.delete(inInfo,"tZMCDFiles.delete");
		for(int i=0;i<inInfo.getBlock(EiConstant.resultBlock).getRowCount();i++)
		{			
			try{
				Documents.deleteLocalFile(outInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdMemoryName"));
			}
			catch(Exception e){
				outInfo.setStatus(-1);
				outInfo.setMsg("删除文件出错：" + e.getMessage());
			}
		}
		inInfo.set("fdForeignId", inInfo.getBlock(EiConstant.resultBlock).getCellStr(0,"fdForeignId"));
		outInfo = query(inInfo);
		return outInfo;
	}
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:29:45 zhangkun
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) { 
		EiInfo out=new EiInfo(); 
		ZMCDFiles _dom = new ZMCDFiles();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CD_FILES")); 
			this.dao.insert("tZMCDFiles.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
}
